//
//  BN_Modify_PassWordViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-5-25.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "BN_Modify_PassWordViewController.h"

@interface BN_Modify_PassWordViewController () {
    BN_MyInfoHelper *myInfoHelper_;
}
@end

@implementation BN_Modify_PassWordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"修改密码";
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVc)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveInfoAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    // return类型
    self.oldPasswordTextField.returnKeyType = UIReturnKeyNext;
    self.nePasswordTextField.returnKeyType = UIReturnKeyNext;
    self.repeatPasswordTextField.returnKeyType = UIReturnKeyDone;
    
    switch (MAIN_VERSION) {
        case 6:
            self.oldPasswordTextField.frame = CGRectMake(CGRectGetMinX(self.oldPasswordTextField.frame), CGRectGetMinY(self.oldPasswordTextField.frame)-64, 280, 45);

            self.nePasswordTextField.frame = CGRectMake(CGRectGetMinX(self.nePasswordTextField.frame), CGRectGetMinY(self.nePasswordTextField.frame)-64, 280, 45);

            self.repeatPasswordTextField.frame = CGRectMake(CGRectGetMinX(self.repeatPasswordTextField.frame), CGRectGetMinY(self.repeatPasswordTextField.frame)-64, 280, 45);

            self.shuomingLabel.frame = CGRectMake(CGRectGetMinX(self.shuomingLabel.frame), CGRectGetMinY(self.shuomingLabel.frame)-64, 251, 21);
            break;
        default:
            break;
    }
    
    // helper
    myInfoHelper_ = [[BN_MyInfoHelper alloc]init];
    
    // 旧密码得到焦点
    [self.oldPasswordTextField becomeFirstResponder];
}

#pragma mark 保存数据
- (void)saveInfoAction {
    
    if (self.oldPasswordTextField.text.length > 0 && self.nePasswordTextField.text.length > 0 && self.repeatPasswordTextField.text.length > 0) {
        
        if ([self.nePasswordTextField.text isEqualToString:self.repeatPasswordTextField.text]) {
            [SVProgressHUD showWithStatus:@"保存中..." maskType:SVProgressHUDMaskTypeBlack];
            NSString *newPass = self.repeatPasswordTextField.text;
            [myInfoHelper_ updateUserInfoWithNewPass:newPass completeHandler:^(id result, NSString *errMsg) {
                if (errMsg) {
                    [SVProgressHUD dismiss];
                    [LZCHINT title:errMsg type:LZCError duration:2 places:HINT_PLACES callBack:NULL];
                    
                }else{
                    [SVProgressHUD dismiss];
                    [LZCHINT title:result type:LZCSuccess duration:2 places:HINT_PLACES callBack:NULL];
                }
            }];

        }else {
            [LZCHINT title:@"两次输入密码不一致" type:LZCInfo duration:2 places:HINT_PLACES callBack:NULL];
            
        }
    }else {
        [SVProgressHUD dismiss];
        [LZCHINT title:@"请输入完整信息" type:LZCInfo duration:2 places:HINT_PLACES callBack:NULL];
    }
}

#pragma mark U ITextField Delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if ([textField isEqual:self.oldPasswordTextField]) {
        [self.nePasswordTextField becomeFirstResponder];
    }
    if ([textField isEqual:self.nePasswordTextField]) {
        [self.repeatPasswordTextField becomeFirstResponder];
    }
    if ([textField isEqual:self.repeatPasswordTextField]) {
        [self saveInfoAction];
    }
    return YES;
}

#pragma mark 资源

- (void)backRootVc {
    [self releaseResource];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)releaseResource {
    
}

- (void)dealloc
{
    [self releaseResource];
    NSLog(@"修改密码释放...");
}
@end
